/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.bk.e2p.ysyb;

import static cn.ac.iie.di.ban.ycl.ybk.e2p.yb.tools.JsonUtil.copyFile;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Stack;
import org.apache.commons.io.FileUtils;


/**
 *
 * @author wmr
 */
public class NewClass {
//    /**
//     * 复制单个文件
//     *
//     * @param oldPath String 原文件路径 如：c:/fqf.txt
//     * @param newPath String 复制后路径 如：f:/fqf.txt
//     * @return boolean
//     */
//    public static void copyFile(String oldPath, String newPath) {
//
//        File from = new File(oldPath);
//        File to = new File(newPath);
//        try {
//            FileUtils.copyFile(from, to);
////            int bytesum = 0;
////            int byteread = 0;
////            File oldfile = new File(oldPath);
////            if (oldfile.exists()) { //文件存在时 
////                InputStream inStream = new FileInputStream(oldPath); //读入原文件 
////                FileOutputStream fs = new FileOutputStream(newPath);
////                byte[] buffer = new byte[1444];
////                int length;
////                while ((byteread = inStream.read(buffer)) != -1) {
////                    bytesum += byteread; //字节数 文件大小 
////                    System.out.println(bytesum);
////                    fs.write(buffer, 0, byteread);
////                }
////                inStream.close();
////            }
//        } catch (Exception e) {
//            System.out.println("error: " + e.getMessage() + e);
//        }
//    }

//    public static void chmodForFolder(String fileName) throws IOException {
//
//        System.out.println("mkdirForFile:" + fileName);
//        String path = fileName.substring(0, fileName.lastIndexOf(File.separator));
//        Process p = Runtime.getRuntime().exec(new String[]{"chmod -R 777 ", path});
//        InputStream error = p.getErrorStream();
//        for (int i = 0; i < error.available(); i++) {
//            System.out.println("" + error.read());
//        }
//    }
    
    public static void main(String[] args) {
        //copyFile and chmodFolder
        try {
            copyFile("/ybkdata/yb/TIEBA/20160918/1d675f3d9c21badf18a1fcc42f867e6e/content.txt", "/ybkdata/test2/hh/jj2/content.txt");
//            chmodForFolder("/ybkdata/test/hh/jj/content.txt");
        } catch (Exception e) {
            System.out.println("error: " + e.getMessage() + e);
        }
    }
    
//    public static  void chmodForFolder(String fileName) throws IOException {
//
//
//        System.out.println("mkdirForFile:"+fileName);
//        String path = fileName.substring(0, fileName.lastIndexOf(File.separator));
//        Stack<String> stack = new Stack<String>();
//        stack.push(path);
//
//        boolean touchRoot=false;
//        while(!stack.isEmpty()){
//            System.out.println("in stack:"+path);
//            if(touchRoot){
//                path = stack.pop();
////                exec("mkdir -p "+ path);
//                exec("chmod 777 -R " + path);
//                continue;
//            }else {
//                path = stack.peek();
//                File dir = new File(path);
//                if (dir.exists()) {
//                    path = path.substring(0, path.lastIndexOf(File.separator));
//                    System.out.println("pushing: "+ path);
//                    stack.push(path);
//                    
////                    stack.pop();
////                    touchRoot = true;
////                    continue;
//                } else {
//                    path = path.substring(0, path.lastIndexOf(File.separator));
//                    System.out.println("pushing: "+ path);
//                    stack.push(path);
//                }
//            }
//        }
//
//    }
//    
//    public static void exec(String cmd) {
//        try {
//            // create a new process
//            System.out.println("Creating Process...");
//            Process p = Runtime.getRuntime().exec(cmd);
//
//            // get the error stream of the process and print it
//            InputStream error = p.getErrorStream();
//            for (int i = 0; i < error.available(); i++) {
//                System.out.println("" + error.read());
//            }
//
//            String s;
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
//            while((s=bufferedReader.readLine()) != null)
//                System.out.println(s);
//             p.waitFor();
//
//            // wait for 10 seconds and then destroy the process
//            p.destroy();
//
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//
//    }
}
